﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WCFMINERP.Factory.Presenter;
using WCFMINERP.Contracts.DataContracts;
using WCFMINERP.COM;
using WCFMINERP.InnerFactoryContracts.DataContracts;
using DevExpress.XtraEditors;
using WCFMINERP.Factory.Report;
using DevExpress.XtraReports.UI;

namespace WCFMINERP.Factory
{
    public partial class UCProductCommit : XtraUserControl
    {
        public UCProductCommit()
        {
            InitializeComponent();
        }
        Dictionary<int, string> dictList = new Dictionary<int, string>();
        OrderInfoPresenter _OrderinfoServer = new OrderInfoPresenter();

        Speech sopeech = new Speech();
        private void UCProductCommit_Load(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(Help.ShowImg))
            {
                checkBox.CheckState = CheckState.Checked;//默认选中
                Help.ShowImg = true.ToString();
            }
            else
            {
                if (Convert.ToBoolean(Help.ShowImg))
                {
                    checkBox.CheckState = CheckState.Checked;
                }
                else
                {
                    checkBox.CheckState = CheckState.Unchecked;
                }
            }

            if (string.IsNullOrEmpty(Help.isPring))
            {
                check_Print.CheckState = CheckState.Checked;//默认选中
                Help.isPring = true.ToString();
            }
            else
            {
                if (Convert.ToBoolean(Help.isPring))
                {
                    check_Print.CheckState = CheckState.Checked;
                }
                else
                {
                    check_Print.CheckState = CheckState.Unchecked;
                }
            }

            lbl_Mess.Text = "";
            if (Convert.ToBoolean(Help.ShowImg))
            {
                layou_ShowImg.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
            }
            else
            {
                layou_ShowImg.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
            }           

            //加载岗位
            if (InnterManagePresenter.UserInfo != null)
            {
                foreach (TechModel model in InnterManagePresenter.UserInfo.TechModelList)
                {
                    com_tech.Properties.Items.Add(model);
                }
                com_tech.SelectedIndex = 0;

                PLX.StoreMode = 1;
                PLX.FillThumbnail = false;
                PLX.SelectionAntialiased = true;
                PLX.EnableLoadEXIFThumbnails = false;
                PLX.ThumbnailDisplayFilter = 7;
                PLX.ThumbnailResampleFilter = 7;
                PLX.EnableResamplingOnMinor = false;
                PLX.EnableAdjustOrientation = true;
                PLX.MultiSelecting = false;
                PLX.ThreadPoolSize = 10;
                PLX.ImageCacheSize = 300;
                PLX.LookAhead = 500;
                PLX.ThumbnailsInternalBorder = false;
                PLX.EnableMultiSelect = true;
                PLX.SelectionColor = Color.FromArgb(240, 108, 0);
                PLX.SelectionStyle = 1;
                PLX.MultiSelectionRegion = false;
                PLX.SoftShadow = false;
                PLX.ImageCacheUseDisk = false;
                PLX.EnableImageCaching = true;
                PLX.Width = 300;
                PLX.Height = 300;
                PLX.GridWidth = -1;
                PLX.ThumbHeight = 150;
                PLX.ThumbWidth = 150;
                PLX.ThumbnailsBorderWidth = 0;
                PLX.Size = new Size(300, 900);
              
                PLX.OnDblClick += PLX_OnDblClick;

            }          
        }

        void PLX_OnDblClick(object sender, EventArgs e)
        {
            frmimages image = new frmimages(dictList[PLX.SelectedImage]);
            image.ShowDialog();
        }

        /// <summary>
        /// 切换岗位
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void com_tech_SelectedIndexChanged(object sender, EventArgs e)
        {
            TechModel models = com_tech.SelectedItem as TechModel;
            InnterManagePresenter.tech = models;
        }

        /// <summary>
        /// 扫描
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void textEdit1_KeyPress(object sender, KeyPressEventArgs e)
        {

            if (string.IsNullOrEmpty(Help.PrintName))
            {
                XtraMessageBox.Show("请先设置打印机!");
                return;
            }           
            if (InnterManagePresenter.tech == null)
            {
                lbl_Mess.Text = "请选择岗位";
                return;
            }

            if (e.KeyChar == (int)Keys.Enter)
            {
                if (!string.IsNullOrEmpty(txt_Code.Text))
                {
                    int Stat = 2;
                    if ((10 - txt_Code.Text.Length) > 0)
                    {

                        int d = (10 - txt_Code.Text.Length - 1);
                        double dd = Math.Pow(10, d);
                        Stat = Convert.ToInt32(Stat * dd);
                        if (Stat == 1)
                        {
                            Stat = 2;
                        }
                        txt_Code.Text = Stat + txt_Code.Text;
                    }
                   

                    string mess = "";
                    string productMess = "";
                    string imgpath = "";
                    OrderInfo OrderModel = _OrderinfoServer.CommitTech(txt_Code.Text.TrimEnd(), InnterManagePresenter.tech.TechId,InnterManagePresenter.UserInfo.UserId,out mess, out imgpath);                    
                    if (OrderModel.Commit)
                    {
                        sopeech.Speak(mess);
                        txt_Code.Text = "";
                        if (Convert.ToBoolean(Help.isPring))   //只有true才打印
                        {
                            try
                            {
                                XtraReportProduct2 xrmLog = new XtraReportProduct2(OrderModel);
                                xrmLog.Print(Help.PrintName);
                            }
                            catch (Exception ex)
                            {
                                XtraMessageBox.Show(ex.Message);
                            }
                        }
                    }
                    else
                    {
                        sopeech.Speak(mess);
                    }
                    txt_Code.SelectAll();
                    lbl_Mess.Text = mess;
                    if (OrderModel.prodinfo != null)
                    {
                        lbl_PrdouctMess.Text = OrderModel.orderBagInfo.ShopName;
                        lbl_CtmName.Text = OrderModel.CtmName;
                        lbl_OrderProductCode.Text = OrderModel.OrderProductCode;
                        lbl_OrderProductName.Text = OrderModel.OrderProductName;
                        lbl_PrdouctName.Text = OrderModel.prodinfo.ProductName;

                        lbl_OrderRemark.Text = OrderModel.Remark;
                        lbl_MnemonicCode.Text = OrderModel.prodinfo.MnemonicCode;
                        lbl_Size.Text = OrderModel.prodinfo.Width + "X" + OrderModel.prodinfo.Height;
                        lbl_ProductDes.Text = OrderModel.prodinfo.ProductDes;
                        lbl_strStatus.Text = OrderModel.strStatus;
                        lbl_PageNum.Text = OrderModel.photoCount.ToString();
                        lbl_OrderTime.Text = OrderModel.orderBagInfo.OrderTime.ToString();
                        lbl_FacthTime.Text = OrderModel.FacthTime.ToString();
                        lbl_Remark.Text = OrderModel.orderBagInfo.Remark;
                        WriteAnalyseLog.WriteLogFile(DateTime.Now.ToString() + OrderModel.OrderPath);
                        if (OrderModel.OrderPath != null)
                        {
                            if (Convert.ToBoolean(Help.ShowImg))   //只有显示图片时才能加载
                            {
                                PLX.Clear();
                                dictList.Clear();
                                BindImg(OrderModel.OrderPath);
                            }
                        }
                    }
                    else
                    {
                        PLX.Clear();
                        dictList.Clear();
                        lbl_PrdouctMess.Text = "";
                    }

                }
            }
        }

        /// <summary>
        /// 绑定图片
        /// </summary>
        /// <param name="orderpath"></param>
        public void BindImg(string orderpath)
        {
            if (System.IO.Directory.Exists(@"" + orderpath))
            {
                string[] files = System.IO.Directory.GetFiles(@"" + orderpath);
                
                foreach (string file in files)
                {
                    if (file.EndsWith(".jpg") || file.EndsWith(".png") || file.EndsWith(".JPG") || file.EndsWith(".PNG"))
                    {
                        int i = PLX.AppendImage();
                        PLX.SetImageFileName(i, file);                        
                        dictList.Add(i, file);
                        PLX.Paint();
                    }
                }
            }
        }

        /// <summary>
        /// 设置是否显示图片
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void checkBox_CheckStateChanged(object sender, EventArgs e)
        {
            if (checkBox.CheckState == CheckState.Checked)
            {
                Help.ShowImg = true.ToString();
            }

            else if (checkBox.CheckState == CheckState.Unchecked)
            {
                Help.ShowImg = false.ToString();
            }
            else
            {
                Help.ShowImg = "";
            }
        }

        /// <summary>
        /// 设置是否打印标签
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void check_Print_CheckStateChanged(object sender, EventArgs e)
        {
            if ( check_Print.CheckState == CheckState.Checked)
            {
                Help.isPring = true.ToString();
            }

            else if (check_Print.CheckState == CheckState.Unchecked)
            {
                Help.isPring = false.ToString();
            }
            else
            {
                Help.isPring = "";
            }
        }

        /// <summary>
        /// 设置打印机
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void but_SettingPrint_Click(object sender, EventArgs e)
        {
            frmSelectPrintName printName = new frmSelectPrintName();
            printName.ShowDialog();
        }

      

    }
}
